﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 

public partial class Mod_StockManage_frm_StockOut : System.Web.UI.Page
{
    private static string strType = "5";//采购退货

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserId = cls_Property.SetOrGetUserID;

        if (strUserId != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserId, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetStockOutInfoByMain(strUserId);
            }
            else
            {
                this.labMessage.Text = "";
                this.lblJs.Text = "";//清空Js脚本
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要删除所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要删除所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底删除将无法恢复数据。您真的要删除所选的项吗？');");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindStockOutByDataView(dst, intPageIndex);
        }
    }

    public void ChildChangPage(object sender, EventArgs e)
    {
        //获取保存的从表中的结果集和DataGrid控件
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetChildResultDataSet;
        DataGrid dgrd = cls_Property.SetOrGetChildDataGridControl;

        if (dst != null && dgrd != null)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = dgrd.CurrentPageIndex;
            int intPageCounts = dgrd.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindStockOutChildByDataView(dgrd, dst, intPageIndex);
        }
    }

    public void AddStockOutInfo(object sender, EventArgs e)
    {
        try
        {
            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            string strUserId = cls_Property.SetOrGetUserID;
            ClsMenuDAL cls_Menu = new ClsMenuDAL();

            if (strUserId != "")
            {
                ImageButton ibtnAdd = (ImageButton)sender;
                int intCommandName = Int32.Parse(ibtnAdd.CommandName);

                if (intCommandName == 0)//打开主表添加面板
                {                                        
                    //获取系统编码
                    string strCode = cls_Menu.GetSystemCode("0", "00", "", 13);

                    if (strCode != "Error")
                    {
                        this.panAddResult.Visible = true;
                        this.ctbAdd0.Text = strCode;
                        this.ctsbAdd0.Text = System.DateTime.Today.ToShortDateString();
                        //this.ctsbAdd1.Text = System.DateTime.Today.ToShortDateString();
                        this.lblJs.Text = "<script>document.getElementById('" + this.ctsbAdd0.ClientID + "_txtBox').focus();</script>";
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('新增记录失败，请重试！')</script>";
                    }  
                }
                else if (intCommandName == 1)//关闭主表添加面板
                {
                    this.panAddResult.Visible = false;
                }
                else if (intCommandName == 2)//保存主表添加记录
                {
                    string Dm_No = this.ctbAdd0.Text.Trim();
                    string Dm_Date = this.ctsbAdd0.Text.Trim();
                    string Ct_Id = this.ctsbAdd1.Text.Trim();//供应商
                    string Wh_Id = this.ctsbAdd2.Text.Trim();//仓库
                    string Dp_Id = this.ctsbAdd3.Text.Trim();
                    string Wk_Id = this.ctsbAdd4.Text.Trim();
                    string Remark = this.ctbAdd1.Text.Trim();
                    
                    if (Ct_Id.Length == 0)
                    {
                        this.labMessage.Text = "<script>alert('供应商不能为空！');</script>";
                    }
                    else if (Wh_Id.Length == 0)
                    {
                        this.labMessage.Text = "<script>alert('仓库不能为空！');</script>";
                    }
                    else
                    {
                        Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrder(strType, Dm_No, DateTime.Parse(Dm_Date),
                            DateTime.Now, Ct_Id, "", "", Wh_Id, Dp_Id, Wk_Id, "", "", "", strUserId, Remark, 0);

                        if (bolIsSucceed)
                        {
                            this.GetStockOutInfoByMain(strUserId);//重新绑定结果                        
                            this.ctsbAdd0.Text = ctsbAdd1.Text = ctsbAdd2.Text = ctsbAdd3.Text = ctsbAdd4.Text = this.ctbAdd1.Text = "";

                            //主表新增后直接打开从表进行新增从表记录 Begin///////////////////////////
                            this.panAddResult.Visible = false;
                            ImageButton ibtn = new ImageButton();
                            ibtn.CommandName = "0";

                            int intItems = this.dgrdResult.Items.Count - 1;

                            for (int i = intItems; i >= 0; i--)
                            {
                                string strRs_DmNO = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                                if (strRs_DmNO == Dm_No)
                                {
                                    ibtn.CommandArgument = i.ToString();
                                    break;
                                }
                            }

                            this.OpenChildResult(ibtn, null);
                            this.AddStockOutInfo(this.ibtnAdd, null);
                            //主表新增后直接打开从表进行新增从表记录 End///////////////////////////
                        }
                        else
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('新增记录失败，请重试！')</script>");
                        }
                    }
                }
                //////////////////////////////////////////从表相关操作/////////////////////////////////////
                else
                {
                    int intIndex = Int32.Parse(this.ibtnAdd.CommandArgument);

                    Panel panChildAddResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panChildAddResult");

                    if (intCommandName == 3)//关闭从表添加面板
                    {
                        panChildAddResult.Visible = false;
                    }
                    else if (intCommandName == 4)//保存从表添加记录
                    {
                        CuTextBox Dm_No = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");
                        CuTextSelectBox Im_No = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");//商品编号
                        CuTextBox Qty = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd4");
                        CuTextBox Agio = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd5");

                        CuTextBox Remark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd8");
                        CuTextBox ctbChildAdd3 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd3");//单价
                        CuTextBox ctbChildAddBatchNo = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAddBatchNo");//批号                                                                                             

                        string strIm_id = Im_No.Text.Trim();//商品编号
                        string strQty = ((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd4")).Text.Trim().ToString();//数量
                        if (strIm_id.Length == 0 || strQty.Length == 0)
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('商品或者数量不能为空！');</script>！";
                        }
                        else if (decimal.Parse(strQty) == 0)
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('数量应大于“0”！');</script>！";
                        }
                        else
                        {
                            Boolean bStart = Boolean.Parse(((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("is_Start")).Text.Trim());//是否启用批号
                            string str_BatchNo = ctbChildAddBatchNo.Text.Trim();
                            if (bStart)//如果启用批号，则必须需入批号。否则不允许保存
                            {
                                if (str_BatchNo.Length == 0)
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('此商品批号不能为空！');</script>！";                                    
                                }
                                else
                                {

                                    Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrderChild(-1,strType, Dm_No.Text,Im_No.Text, decimal.Parse(Qty.Text),
                                        decimal.Parse(ctbChildAdd3.Text),0,ctbChildAddBatchNo.Text.Trim(),Remark.Text, 0);

                                    ctbChildAddBatchNo.Text = "";
                                    if (bolIsSucceed)
                                    {                                        
                                        Im_No.Text = Qty.Text = Remark.Text = "";

                                        DataSet dst = cls_StockManage.GetStockOrderChilds(Dm_No.Text, strType);//重新绑定从表结果

                                        if (dst != null && dst.Tables[0].Rows.Count > 0)
                                        {
                                            DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");

                                            this.BindStockOutChildByDataView(dgrd, dst, 0);
                                        }

                                        this.lblJs.Text = "<script>document.getElementById('" + Im_No.ClientID + "_txtBox').focus();</script>";
                                    }
                                    else
                                    {                                        
                                        this.labMessage.Text = "<script language='javascript'>alert('当前商品信息已存在！');</script>！";
                                    }
                                }
                            }
                            else
                            {
                                Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrderChild(-1,strType, Dm_No.Text, Im_No.Text, decimal.Parse(Qty.Text),
                                        decimal.Parse(ctbChildAdd3.Text),0,ctbChildAddBatchNo.Text.Trim(),Remark.Text, 0);

                                ctbChildAddBatchNo.Text = "";
                                if (bolIsSucceed)
                                {
                                    Agio.Text = "1";//清空上一次新增后的值
                                    Im_No.Text = Qty.Text = Remark.Text = "";

                                    DataSet dst = cls_StockManage.GetStockOrderChilds(Dm_No.Text, strType);//重新绑定从表结果

                                    if (dst != null && dst.Tables[0].Rows.Count > 0)
                                    {
                                        DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");

                                        this.BindStockOutChildByDataView(dgrd, dst, 0);
                                    }

                                    this.lblJs.Text = "<script>document.getElementById('" + Im_No.ClientID + "_txtBox').focus();</script>";
                                }
                                else
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('当前商品信息已存在！');</script>！";
                                }
                            }
                        }
                    }
                    else if (intCommandName == 5)//打开从表添加面板
                    {
                        panChildAddResult.Visible = true;

                        CuTextSelectBox ctsbChildAdd0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");
                        this.lblJs.Text = "<script>document.getElementById('" + ctsbChildAdd0.ClientID + "_txtBox').focus();</script>";
                    }
                }
            }
            else
            {
                this.Page.Session.Remove("SetOrGetUserID");
                this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
            }
        }
        catch
        {
            this.labMessage.Text = "<script language='javascript'>alert('请输入必填项或正确的格式！');</script>！";
        }
    }

    public void UpDateStockOutInfo(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserId = cls_Property.SetOrGetUserID;

        if (strUserId != "")
        {
            ImageButton ibtnUpdate = (ImageButton)sender;
            int intCommandName = Int32.Parse(ibtnUpdate.CommandName);
            int intIndex = Int32.Parse(ibtnUpdate.CommandArgument);

            if (intCommandName == 0)//主表确认
            {
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        string strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                        ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();                        
                        Boolean bolIsSucceed = cls_StockManage.UpdateStockOrderState(strDm_No, 0, strUserId, strType);
                        if (!bolIsSucceed)
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('确认失败，请重试！')</script>");
                        }
                    }
                }

                this.GetStockOutInfoByMain(strUserId);//重新绑定结果
            }
            else if (intCommandName == 3)//主表取消确认
            {
                ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
                string strDm_No = ((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0")).Text.Trim();
               
                Boolean bolIsSucceed = cls_StockManage.UpdateStockOrderState(strDm_No, 1, strUserId, strType);
                if (bolIsSucceed)
                {
                    this.GetStockOutInfoByMain(strUserId);//重新绑定结果
                }
                else
                {
                    this.Page.RegisterClientScriptBlock("Js", "<script>alert('取消确认失败，请重试！')</script>");
                }

            }
            else if (intCommandName == 1 || intCommandName == 2 || intCommandName == 4)//主表编辑
            {
                CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
                ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
                ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
                ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
                ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
                CuTextBox ctbRs0 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");
                CuTextBox ctbRs1 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs1");
                CuTextSelectBox ctsbRs0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs0");
                CuTextSelectBox ctsbRs1 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs1");
                CuTextSelectBox ctsbRs2 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs2");//仓库
                CuTextSelectBox ctsbRs3 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs3");
                CuTextSelectBox ctsbRs4 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs4");

                Label labId = (Label)this.dgrdResult.Items[intIndex].FindControl("labId");
                if (intCommandName == 1)//取消编辑
                {
                    this.GetStockOutInfoByMain(strUserId);//重新绑定结果
                }
                else if (intCommandName == 2)//编辑
                {
                    //ctbRs0.Text = ctbRs0.ToolTip;
                    labId.Text = labId.ToolTip;
                    ctbRs1.Text = ctbRs1.ToolTip;
                    ctsbRs0.Text = ctsbRs0.ToolTip;
                    ctsbRs1.Text = ctsbRs1.ToolTip;
                    ctsbRs2.Text = ctsbRs2.ToolTip;
                    ctsbRs3.Text = ctsbRs3.ToolTip;
                    ctsbRs4.Text = ctsbRs4.ToolTip;
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                    chkRsSelect.Visible = ibtnRsEdit.Visible = ibtnRsOAudit.Visible = false;
                    ctbRs1.ReadOnly = ctsbRs0.ReadOnly = ctsbRs1.ReadOnly = ctsbRs2.ReadOnly = ctsbRs3.ReadOnly = ctsbRs4.ReadOnly = false;
                    this.lblJs.Text = "<script>document.getElementById('" + ctsbRs0.ClientID + "_txtBox').select();document.getElementById('" + ctsbRs0.ClientID + "_txtBox').focus();</script>";
                }
                else//保存编辑
                {
                    ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
                    Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrder(strType, ctbRs0.Text.Trim(), DateTime.Parse(ctsbRs0.Text.Trim()),
                        DateTime.Now, ctsbRs1.Text.Trim(), "", "", ctsbRs2.Text.Trim(), ctsbRs3.Text.Trim(),
                        ctsbRs4.Text.Trim(), "", "", "", strUserId, ctbRs1.Text.Trim(), 1);

                    if (bolIsSucceed)
                    {
                        this.GetStockOutInfoByMain(strUserId);//重新绑定结果
                    }
                    else
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('修改记录失败，请重试！')</script>");
                    }
                }
            }
            //////////////////////////////////////////从表相关操作/////////////////////////////////////
            else
            {
                ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
                DataGrid dgrd = new DataGrid();
                string strDm_No = "";
                int intItems = this.dgrdResult.Items.Count;

                for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                {
                    Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                    if (panItem.Visible)
                    {
                        dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                        strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                        break;
                    }
                }

                CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[intIndex].FindControl("chkChildRsSelect");//在子DataGrid中找要操作的控件
                ImageButton ibtnChildRsCancel = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsCancel");
                ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsEdit");
                ImageButton ibtnChildRsServer = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsServer");

                CuTextSelectBox ctsbChildRs0 = (CuTextSelectBox)dgrd.Items[intIndex].FindControl("ctsbChildRs0");
                CuTextBox ctbPrice = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs3");//单价
                CuTextBox ctbQty = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs4");//数量
                CuTextBox ctbChildRs5 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs5");
                CuTextBox ctbChildRs8 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs8");

                CuTextBox ctbChildRsBatchNo = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsBatchNo");//批号                

                CuTextSelectBox ctbChildRsStoreId = (CuTextSelectBox)dgrd.Items[intIndex].FindControl("ctbChildRsStoreId");
                if (intCommandName == 5)//取消编辑
                {
                    DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);

                    if (dst != null && dst.Tables[0].Rows.Count > 0)
                    {
                        this.BindStockOutChildByDataView(dgrd, dst, 0);
                    }
                }
                else if (intCommandName == 6)//编辑
                {                    
                    chkChildRsSelect.Visible = ibtnChildRsEdit.Visible = false;
                    ibtnChildRsCancel.Visible = ibtnChildRsServer.Visible = true;
                    ctbChildRsBatchNo.ReadOnly = ctbQty.ReadOnly = false;
                    this.lblJs.Text = "<script>document.getElementById('" + ctbQty.ClientID + "_txtBox').select();document.getElementById('" + ctbQty.ClientID + "_txtBox').focus();</script>";
                }
                else//保存编辑
                {
                    int iId = int.Parse(dgrd.Items[intIndex].Cells[1].Text.Trim());

                    Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrderChild(iId,strType, strDm_No, ctsbChildRs0.Text,
                        decimal.Parse(ctbQty.Text), decimal.Parse(ctbPrice.Text.Trim()),0, ctbChildRsBatchNo.Text, ctbChildRs8.Text, 1);

                    if (bolIsSucceed)//重新绑定从表结果
                    {
                        DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);

                        if (dst != null && dst.Tables[0].Rows.Count > 0)
                        {
                            this.BindStockOutChildByDataView(dgrd, dst, 0);
                        }
                    }
                    else
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('修改记录失败，请重试！')</script>");
                    }
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    public void OpenChildResult(object sender, ImageClickEventArgs e)
    {
        string strUserID = this.getCurUserId();
        ImageButton ibtnOpen = (ImageButton)sender;
        int intCommandName = Int32.Parse(ibtnOpen.CommandName);
        int intIndex = Int32.Parse(ibtnOpen.CommandArgument);
        int intCount = this.dgrdResult.Items.Count;
        bool is_audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text);

        CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
        ImageButton ibtnOpenChild = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
        ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
        Panel panShowChildResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panShowChildResult");


        CuTextBox ctbRs0 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");//显示从表信息
        ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
        DataSet dst = cls_StockManage.GetStockOrderChilds(ctbRs0.Text.Trim(), strType);        

        if (dst == null || dst.Tables[0].Rows.Count <= 0)
        {
            ibtnOpen.Attributes.Add("OnClick", "return confirm('单据录入未完成，您要删除此单据吗？');");
        }

        if (intCommandName == 0)//展开
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");
                this.BindStockOutChildByDataView(dgrd, dst, 0);//绑定从表信息
            }

            for (int i = 0; i < intCount; i++)//找到展开的项
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (i == intIndex)
                {
                    panItem.Visible = true;
                }
                else
                {
                    panItem.Visible = false;
                }
            }

            this.ibtnAdd.CommandName = "5";//主表展开后则不可操作
            this.ibtnRef.CommandName = "1";
            this.ibtnAdd.CommandArgument = intIndex.ToString();
            panShowChildResult.Visible = true;
            ibtnOpenChild.CommandName = "1";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/10.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/16.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/18.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_3.gif";
            this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;

            if (is_audit)//如果主表确认后则从表不可操作
            {
                this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = false;
                this.ibtnAdd.ImageUrl = "../App_Images/b_18_3.gif";
                this.ibtnDelete0.ImageUrl = "../App_Images/b_19_5.gif";
                this.imgDelete.ImageUrl = "../App_Images/b_19_6.gif";
                this.imgDelete.Attributes.Add("OnMouseOver", "");
                this.imgDelete.Attributes.Add("OnMouseOut", "");
                this.imgDelete.Attributes.Add("OnClick", "");
            }            
        }
        else//收缩
        {
            this.ibtnAdd.CommandName = "0";//收缩后都可操作
            this.ibtnRef.CommandName = "0";
            panShowChildResult.Visible = false;
            ibtnOpenChild.CommandName = "0";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/9.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/11.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/8.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/4.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_1.gif";
            this.ibtnAdd.ImageUrl = "../App_Images/b_18_1.gif";
            this.ibtnDelete0.ImageUrl = "../App_Images/b_19_1.gif";
            this.imgDelete.ImageUrl = "../App_Images/b_19_4.gif";
            this.imgDelete.Attributes.Add("OnMouseOver", "ChangImage(this, '../App_Images/b_19_3.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_2.gif')");
            this.imgDelete.Attributes.Add("OnMouseOut", "ChangImage(this, '../App_Images/b_19_4.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_1.gif')");
            this.imgDelete.Attributes.Add("OnClick", "ChildMenuVisible()");
            this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = true;

            if (dst == null || dst.Tables[0].Rows.Count <= 0)
            {
                Boolean bolIsSucceed = cls_StockManage.DeleteStockOrderState(ctbRs0.Text.Trim(), 3, strType);
                this.GetStockOutInfoByMain(strUserID);
            }
            else
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();//重新绑定数据
                dst = cls_Property.SetOrGetCorporationDataSet;
                this.BindStockOutByDataView(dst, this.dgrdResult.CurrentPageIndex);
            }
        }
    }

    private void GetStockOutInfoByMain(string strUserId)
    {
        ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
        DataSet dst = cls_StockManage.GetStockOrderMains(5,strUserId);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindStockOutByDataView(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        }
    }

    private void BindStockOutByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
        else
        {
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
         }
    }

    private void BindStockOutChildByDataView(DataGrid dgrd, DataSet dst, int CurrPageIndex)
    {
        if (dgrd != null && dst != null)
        {
            dgrd.CurrentPageIndex = CurrPageIndex;
            dgrd.DataSource = dst;
            dgrd.DataBind();

            int intIndex = Int32.Parse(dgrd.ToolTip);
            int intItems = dgrd.Items.Count;

            Label lblChildRsCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildRsCounts");
            Label lblChildPageCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildPageCounts");
            Label lblChildCurrPage = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildCurrPage");
            LinkButton lbtnChildFirst = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildFirst");
            LinkButton lbtnChildPrev = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildPrev");
            LinkButton lbtnChildNext = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildNext");
            LinkButton lbtnChildEnd = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildEnd");

            lblChildRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条明细记录";
            lblChildPageCounts.Text = "共" + dgrd.PageCount + "页";
            lblChildCurrPage.Text = "当前第" + (dgrd.CurrentPageIndex + 1) + "页";
            lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = true;
            lblChildRsCounts.Enabled = lblChildPageCounts.Enabled = lblChildCurrPage.Enabled = true;

            if (dgrd.PageCount == 1)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == dgrd.PageCount - 1)
            {
                lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == 0)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = false;
            }

            //判断从表中的记录是否可操作
            //for (int i = 0; i < intItems; i++)
            //{
            //    CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[i].FindControl("chkChildRsSelect");
            //    ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[i].FindControl("ibtnChildRsEdit");

            //    if (!this.ibtnAdd.Enabled)
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            //    }
            //    else
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = true;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            //    }
            //}

            //保存从表中的结果集和DataGrid控件用于从表中的翻页
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetChildResultDataSet = dst;
            cls_Property.SetOrGetChildDataGridControl = dgrd;
        }
    }

    private void DeleteStockCheckInfo(int CommandName)
    {
        string strUserId = this.getCurUserId();

        if (strUserId != "")
        {
            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            int intCount = this.dgrdResult.Items.Count;
            int intcommand = Int32.Parse(this.ibtnRef.CommandName);

            if (intcommand == 0)//删除主表
            {
                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        Boolean bIs_audit = Boolean.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());//确认状态
                        Boolean bIs_end = Boolean.Parse(this.dgrdResult.Items[i].Cells[2].Text.Trim());//终止状态

                        Boolean bolIsSucceed = false;
                        string strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                        if (bIs_audit)
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('此单已确认，不能删除！');</script>！";
                        }
                        else if (bIs_end)
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('此单已终止，不能删除！');</script>！";
                        }
                        else if (CommandName == 0)//不彻底删除
                        {
                            bolIsSucceed = cls_StockManage.DeleteStockOrderState(strDm_No, 2, strType);
                            if (!bolIsSucceed)
                            {
                                this.Page.RegisterClientScriptBlock("Js", "<script>alert('删除记录失败，请重试！')</script>");
                            }
                        }
                        else//彻底删除
                        {
                            bolIsSucceed = cls_StockManage.DeleteStockOrderState(strDm_No, 3, strType);
                            if (!bolIsSucceed)
                            {
                                this.Page.RegisterClientScriptBlock("Js", "<script>alert('删除记录失败，请重试！')</script>");
                            }
                        }

                        
                    }
                }

                //重新绑定主表结果
                this.GetStockOutInfoByMain(strUserId);
            }
            else//删除从表
            {
                DataGrid dgrd = new DataGrid();
                string strDm_No = "";
                int intItems = this.dgrdResult.Items.Count;

                for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                {
                    Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                    if (panItem.Visible)
                    {
                        dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                        strDm_No = ((Label)this.dgrdResult.Items[i].FindControl("labId")).Text.Trim();//ctbRs0
                        break;
                    }
                }

                int intChildItems = dgrd.Items.Count;//循环删除所选的项

                for (int j = intChildItems - 1; j >= 0; j--)
                {
                    bool IsChecked = ((CheckBox)dgrd.Items[j].FindControl("chkChildRsSelect")).Checked;

                    if (IsChecked)
                    {
                        string strN_No = dgrd.Items[j].Cells[1].Text.Trim();
                        Boolean bolIsSucceed = false;

                        if (CommandName == 0)//不彻底删除
                        {
                            bolIsSucceed = cls_StockManage.DeleteStockOrderChildState(strDm_No, strN_No, 2, strType);
                        }
                        else//彻底删除
                        {
                            bolIsSucceed = cls_StockManage.DeleteStockOrderChildState(strDm_No, strN_No, 3, strType);
                        }

                        if (!bolIsSucceed)
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('删除记录失败，请重试！')</script>");
                        }
                    }
                }

                DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);//重新绑定从表结果

                if (dst != null && dst.Tables[0].Rows.Count > 0)
                {
                    this.BindStockOutChildByDataView(dgrd, dst, 0);
                }
                else
                {
                    dgrd.DataSource = null;
                    dgrd.DataBind();
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }   

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindStockOutByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入正确的页码数！')</script>");
                    }
                }
                catch
                {
                    this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入数字类型的页码！')</script>");
                }
            }
            else
            {
                this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入要跳转的页码！')</script>");
            }
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        string strUserId = this.getCurUserId();
        int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

        if (intCommand == 0)//刷新主表
        {
            this.GetStockOutInfoByMain(strUserId);
        }
        else//刷新从表
        {
            DataGrid dgrd = new DataGrid();
            string strDm_No = "";
            int intItems = this.dgrdResult.Items.Count;

            for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (panItem.Visible)
                {
                    dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                    strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                    break;
                }
            }

            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.BindStockOutChildByDataView(dgrd, dst, 0);
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            bool Is_audit = bool.Parse(e.Item.Cells[1].Text.Trim());//确认状态
            bool Is_end = bool.Parse(e.Item.Cells[2].Text.Trim());//终止状态
            bool Is_visible = bool.Parse(e.Item.Cells[3].Text.Trim());//是否作废

            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            CuTextBox ctbRs1 = (CuTextBox)e.Item.FindControl("ctbRs1");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)e.Item.FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");
            ImageButton ibtnRsOAudit = (ImageButton)e.Item.FindControl("ibtnRsOAudit");

            //修改按回车提交
            if (ctbRs1.ReadOnly)
            {
                ctbRs1.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnRsServer.ClientID + "').click();}");
            }

            if (Is_visible)//是否作废
            {
                chkRsSelect.Enabled = ibtnRsEdit.Enabled = false;
                ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }

            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;
            chkRsSelect.Visible = true;

            if (!Is_audit)
            {
                chkRsSelect.Visible = true;
                ibtnRsEdit.Visible = true;

                if (strButtonEdit.Trim() == "1")
                {
                    if (this.panAudit.Visible || this.panDelete.Visible)
                    {
                        chkRsSelect.Visible = true;
                    }

                    ibtnRsEdit.Visible = true;
                }
                else if (this.panAudit.Visible || this.panDelete.Visible)
                {
                    chkRsSelect.Visible = true;
                }
            }
            else
            {
                ibtnRsOAudit.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnRsOAudit.Enabled = false;
                    ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }
            }

            //判断是否已终止
            if (Is_end)
            {
                ibtnRsEdit.Visible = false;
                ibtnRsCancel.Visible = false;
                ibtnRsServer.Visible = false;
                chkRsSelect.Visible = true;
                ibtnRsOAudit.Visible = true;
                ibtnRsOAudit.Enabled = false;
                ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                ibtnRsOAudit.ToolTip = "此单已终止";

                if (!this.panAudit.Visible && !this.panDelete.Visible)//&& !this.panEnd.Visible 
                {
                    chkRsSelect.Enabled = false;
                }
            }
        }
    }

    protected void dgrdResult_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType.ToString() == "Item" || e.Item.ItemType.ToString() == "AlternatingItem")
        {
            DataGrid dgrdChildResult = (DataGrid)e.Item.FindControl("dgrdChildResult");

            //子DataGrid注册事件
            dgrdChildResult.ItemDataBound += new DataGridItemEventHandler(dgrdChildResult_ItemDataBound);
        }
    }

    protected void dgrdChildResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            int iIndex = int.Parse(((DataGrid)sender).ToolTip);
            bool Is_audit = bool.Parse(this.dgrdResult.Items[iIndex].Cells[1].Text.Trim());//是否确认
            bool Is_visible = bool.Parse(this.dgrdResult.Items[iIndex].Cells[3].Text.Trim());//是否作废
            CheckBox chkChildRsSelect = (CheckBox)e.Item.FindControl("chkChildRsSelect");
            ImageButton ibtnChildRsEdit = (ImageButton)e.Item.FindControl("ibtnChildRsEdit");
            if (Is_audit || Is_visible)//是否作废
            {
                chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
                ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }
        }     
    }

    protected void ibtnEnd_Click(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserId = cls_Property.SetOrGetUserID;

        if (strUserId != "")
        {
            int intCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intCount; i++)
            {
                bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                if (IsChecked)
                {
                    string strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                    ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
                    Boolean bolIsSucceed = cls_StockManage.EndBOD_no(strType, "tboDueM", "dm_no", strDm_No, "dm_type");

                    if (!bolIsSucceed)
                    {
                        this.lblJs.Text = "<script>alert('终止失败，请重试！')</script>";
                    }
                }
            }

            //重新绑定主表结果
            this.GetStockOutInfoByMain(strUserId);
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    //根据订单状态进行相关查询
    public void lbtnQuery_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.QueryOrderByStatu(intCommandName);
    }

    private void QueryOrderByStatu(int CommandName)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserId = cls_Property.SetOrGetUserID;

        if (strUserId != "")
        {
            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            DataSet dst = cls_StockManage.GetOrderInformationByStatu(strUserId,"tboDueM", int.Parse(strType), CommandName);

            cls_Property.SetOrGetCorporationDataSet = dst;
            this.BindStockOutByDataView(dst, 0);
        }
    }

    //得到当前用户ID(公用)
    private string getCurUserId()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserId = cls_Property.SetOrGetUserID;
        if (strUserId.Length > 0)
        {
            return strUserId;
        }
        else
        {
            return "";
        }
    }
}
